package deliciouse.servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import deliciouse.dao.MessagesDao;
import deliciouse.dao.impl.MessagesDaoImpl;
import deliciouse.entity.Messages;

@WebServlet("/AddMessage")
public class MessageAddServlet extends HttpServlet{
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		MessagesDao messagesdao = new MessagesDaoImpl();
		String username = null;
		String phone = null;
		String content = null;
		String email = null;
		if (request.getParameter("username") != null && !request.getParameter("username").isEmpty())
			username = request.getParameter("username");

		if (request.getParameter("phone") != null && !request.getParameter("phone").isEmpty())
			phone = request.getParameter("phone");

		if (request.getParameter("content") != null && !request.getParameter("content").isEmpty())
			content = request.getParameter("content");

		if (request.getParameter("email") != null && !request.getParameter("email").isEmpty())
			email = request.getParameter("email");
		Date publishdate =  new Date();
		Messages messages = new Messages();
		messages.setUsername(username);
		messages.setContent(content);
		messages.setPhone(phone);
		messages.setEmail(email);
		messages.setPublishdate(publishdate);
		messagesdao.addMessage(messages);
		response.sendRedirect("Message");
	}
}
